package com.fulihui.wgj.admin.integration.datastaits.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.PageViewBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.util.DateUtils;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.mass.request.MassDataStatisDetailQueryRequest;
import com.fulihui.wgj.admin.api.mass.request.MassDataStatisQueryRequest;
import com.fulihui.wgj.admin.api.mass.vo.MassDataStatisDetailVO;
import com.fulihui.wgj.admin.api.mass.vo.MassDataStatisVO;
import com.fulihui.wgj.admin.integration.datastaits.MassDataStatisApiClient;
import com.fulihui.wgj.datastatis.facade.api.mass.MassDataStatisApi;
import com.fulihui.wgj.datastatis.facade.dto.MassDataStatisDTO;
import com.fulihui.wgj.datastatis.facade.dto.MassDataStatisDetailDTO;
import com.fulihui.wgj.datastatis.facade.request.MassDataStatisDetailRequest;
import com.fulihui.wgj.datastatis.facade.request.MassDataStatisPageRequest;
import com.fulihui.wgj.datastatis.facade.result.MassDataStatisDetailResult;
import com.fulihui.wgj.datastatis.facade.result.MassDataStatisPageResult;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.List;

/**
 * @author: Levon
 * @date: 2018-08-03 11:18
 */
@Service
public class MassDataStatisApiClientImpl implements MassDataStatisApiClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(MassDataStatisApiClientImpl.class);

    @Reference(version = "1.0.0")
    private MassDataStatisApi massDataStatisApi;

    @Override
    public PageView<MassDataStatisVO> queryMassDataStatisPage(MassDataStatisQueryRequest request) {

        List<MassDataStatisVO> statisVOS = Lists.newArrayList();

        MassDataStatisPageRequest pageRequest = new MassDataStatisPageRequest();
        pageRequest.setAppId(request.getAppId());
        try {
            pageRequest.setEndRefDate(DateUtils.parseWebFormat(request.getEndRefDate()));
            pageRequest.setStartRefDate(DateUtils.parseWebFormat(request.getStartRefDate()));
        } catch (ParseException e) {
            LOGGER.error("queryMassDataStatisPage::日期转换错误", e);
        }
        pageRequest.setTitle(request.getTitle());
        pageRequest.setPage(request.getPage());
        pageRequest.setRows(request.getRows());
        MassDataStatisPageResult pageResult = massDataStatisApi.queryMassDataStatisPage(pageRequest);
        if (RpcResultCheckUtil.isSuccess(pageResult)) {
            List<MassDataStatisDTO> values = pageResult.getValues();
            if (!CollectionUtils.isEmpty(values)) {
                for (MassDataStatisDTO value : values) {
                    MassDataStatisVO statisVO = new MassDataStatisVO();
                    statisVO.setId(value.getId());
                    statisVO.setMsgid(value.getMsgid());
                    statisVO.setTitle(value.getTitle());
                    statisVO.setRefDate(DateUtils.formatWebFormat(value.getRefDate()));
                    if (!StringUtils.isEmpty(value.getMsgid())) {
                        String[] msgdataid = value.getMsgid().split("_");
                        if (msgdataid != null && msgdataid.length > 1) {
                            statisVO.setIndex(msgdataid[1]);
                        }
                    }
                    statisVOS.add(statisVO);
                }
            }
        }
        return PageViewBuilder.build(statisVOS, pageResult.getPage(), pageResult.getRows(), pageResult.getTotalRows());
    }

    @Override
    public List<MassDataStatisDetailVO> queryMassDataDetail(MassDataStatisDetailQueryRequest request) {

        List<MassDataStatisDetailVO> detailVOS = Lists.newArrayList();

        MassDataStatisDetailRequest detailRequest = new MassDataStatisDetailRequest();
        detailRequest.setAppid(request.getAppId());
        detailRequest.setMsgid(request.getMsgid());
        try {
            detailRequest.setRefDate(DateUtils.parseWebFormat(request.getRefDate()));
        } catch (ParseException e) {
            LOGGER.error("queryMassDataStatisPage::日期转换错误", e);
        }
        MassDataStatisDetailResult result = massDataStatisApi.queryMassDataStatisDetail(detailRequest);
        if (RpcResultCheckUtil.isSuccess(result)) {
            List<MassDataStatisDetailDTO> values = result.getValues();
            if (!CollectionUtils.isEmpty(values)) {
                for (MassDataStatisDetailDTO value : values) {
                    MassDataStatisDetailVO detailVO = BeanConv.copy(value, MassDataStatisDetailVO.class);
                    detailVO.setStatDate(DateUtils.formatWebFormat(value.getStatDate()));
                    detailVOS.add(detailVO);
                }
            }
        }
        return detailVOS;
    }
}
